package com.yitao.cms.service.order;

import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.order.OrderDeliveryPackageDto;
import com.yitao.client.model.order.OrderDeliveryPackageModel;
import com.yitao.client.model.order.QOrderDeliveryPackageModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;

/**
 * @Auther: TUO
 * @Date: 2019/7/3 15:30
 * @Description:
 */
@Service
public class OrderDeliveryPackageService extends GenericEntityService<OrderDeliveryPackageModel, OrderDeliveryPackageDto, QOrderDeliveryPackageModel> {
    public OrderDeliveryPackageService(EntityManager entityManager) {
        super(entityManager);
    }

    public List<OrderDeliveryPackageModel> getOrderPackageInfo(Long deliveryId) {
        return queryFactory.select(Q).from(Q).where(Q.deliveryId.eq(deliveryId)).fetch();
    }

    /**
     * 添加包裹信息
     *
     * @param orderDeliveryPackageModel
     */
    @Transactional
    public void save(OrderDeliveryPackageModel orderDeliveryPackageModel) {
        repository.save(orderDeliveryPackageModel);
    }

    @Transactional
    public void saveList(List<OrderDeliveryPackageModel> orderDeliveryPackageModelList) {
        repository.save(orderDeliveryPackageModelList);
    }

    public OrderDeliveryPackageModel getOrderPackage(Long orderDetailId) {
        List<OrderDeliveryPackageModel> fetch = queryFactory.select(Q).from(Q).where(Q.orderDetailId.eq(orderDetailId)).fetch();
        if (fetch != null && fetch.size() >= 1) {
            return fetch.get(0);
        }
        return null;
    }
}
